package com.eleMall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eleMall.dto.marketDto;
import com.eleMall.entity.Market;
import com.eleMall.entity.Result;
import com.eleMall.service.MarketService;
import com.eleMall.util.getMarkets;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (com.eleMall.entity.Market)表控制层
 *
 * @author makejava
 * @since 2024-03-20 20:50:28
 */
@Slf4j
@RestController
@RequestMapping("/eleMall/market")
@Api(tags = "(com.eleMall.entity.Market)表控制层")
public class MarketController {
    /**
     * 服务对象
     */
    @Resource
    private MarketService marketService;

    /**
     * 全查询
     *
     * @return 查询结果
     */
    @ApiOperation("全查询")
    @GetMapping("/all")
    public Result queryAll() throws Exception {
        log.info("正在请求全查询");
        List<marketDto> marketDtos = new ArrayList<marketDto>();
        List<Market> list1 = marketService.list();
        //新数据
        List<Market> markets = getMarkets.getMarkets();
        for (int i = 0; i < markets.size(); i++) {
            Market market = markets.get(i);
            marketDto marketDto = new marketDto();
            marketDto.setProdName(market.getProdName());
            marketDto.setLowPrice(market.getLowPrice());
            marketDto.setHighPrice(market.getHighPrice());
            marketDto.setAvgPrice(market.getAvgPrice());
            Market market1 = null;
            for (int j = list1.size()-1; j >= 0 ; j--) {
                Market temp = list1.get(j);
                if(temp.getProdName().equals(market.getProdName()) && temp.getSpecInfo().equals(market.getSpecInfo())){
                    market1 = temp;
                    break;
                }
            }
            if(market1 != null){
                marketDto.setChange((market.getAvgPrice()-market1.getAvgPrice()) / market1.getAvgPrice());
            }else {
                marketDto.setChange(0.0);
            }
            marketDtos.add(marketDto);
        }

        return Result.success(marketDtos);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("{id}")
    public Result<Market> queryById(@ApiParam(value = "id 主键") @PathVariable("id") Long id) {
        Market market = marketService.getById(id);
        return Result.success(market);
    }

    /**
     * 新增数据
     *
     * @param market 实体
     * @return 新增结果
     */
    @ApiOperation("新增数据")
    @PostMapping
    public Result add(@ApiParam(value = "market 实体") @RequestBody Market market) {
        marketService.save(market);
        return Result.success("新增成功");
    }

    /**
     * 编辑数据
     *
     * @param market 实体
     * @return 编辑结果
     */
    @ApiOperation("编辑数据")
    @PutMapping
    public Result edit(@ApiParam(value = "market 实体") @RequestBody Market market) {
        marketService.updateById(market);
        return Result.success("新增成功");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("删除数据")
    @DeleteMapping
    public Result deleteById(@ApiParam(value = "id 主键") Long id) {
        marketService.removeById(id);
        return Result.success("删除成功");
    }

}


